package com.example.zhu95.mytimber.listener;

import android.view.View;

import com.example.zhu95.mytimber.ConstantValue;
import com.example.zhu95.mytimber.MyApplication;
import com.example.zhu95.mytimber.activity.BaseActivity;
import com.example.zhu95.mytimber.entity.PlayQueueRecord;
import com.example.zhu95.mytimber.entity.SongBean;
import com.example.zhu95.mytimber.entity.SongData;
import com.example.zhu95.mytimber.utils.Util;

import java.util.List;

import static com.example.zhu95.mytimber.ConstantValue.PLAYMODE_RANDOM;
import static com.example.zhu95.mytimber.ConstantValue.PLAYMODE_SEQUENCE;
import static com.example.zhu95.mytimber.ConstantValue.PLAYMODE_SINGLE;

/**
 * @author zhu95
 */
public class OnPlayPreviousSongClickListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        int playMode = PlayQueueRecord.Companion.getPlayModeFromDB();
        List<SongBean> playqueueSongs = PlayQueueRecord.Companion.getPlayqueueSongs();
        int index = playqueueSongs.indexOf(SongData.findSongByID(BaseActivity.songid));
        switch (playMode) {
            case PLAYMODE_SINGLE:
                break;
            case PLAYMODE_RANDOM:
                int num = index;
                //如果当前播放列表的歌曲数为1 或 o
                if (playqueueSongs.size() < 2) {
                    MyApplication.sendPlayMusicCmdBroadcast(BaseActivity.songid, null, ConstantValue.PlayMusicCmd_Play, -1);
                    return;
                }
                while (num == index) {
                    num = Util.INSTANCE.getRandomPositiveNum();
                    num = num % playqueueSongs.size();
                }
                index = num;
                break;
            case PLAYMODE_SEQUENCE:
                index = (index - 1) % playqueueSongs.size();
                if (index < 0) {
                    index = playqueueSongs.size() - 1;
                }
                break;
            default:
        }
        MyApplication.sendPlayMusicCmdBroadcast(playqueueSongs.get(index).getId(), null, ConstantValue.PlayMusicCmd_Play, 0);
    }
}
